{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "11cc2042",
   "metadata": {},
   "source": [
    "# Lesson 2: Embeddings"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14c47bc7",
   "metadata": {},
   "source": [
    "### Setup\n",
    "Load needed API keys and relevant Python libaries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8831c1e6",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "# !pip install cohere umap-learn altair datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23d4483b",
   "metadata": {
    "height": 63
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "_ = load_dotenv(find_dotenv()) # read local .env file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14cee683",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "import cohere\n",
    "co = cohere.Client(os.environ['COHERE_API_KEY'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7cd186b9",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a66e9572",
   "metadata": {},
   "source": [
    "## Word Embeddings\n",
    "\n",
    "Consider a very small dataset of three words."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ab6a806",
   "metadata": {
    "height": 148
   },
   "outputs": [],
   "source": [
    "three_words = pd.DataFrame({'text':\n",
    "  [\n",
    "      'joy',\n",
    "      'happiness',\n",
    "      'potato'\n",
    "  ]})\n",
    "\n",
    "three_words"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e27c4adb",
   "metadata": {},
   "source": [
    "Let's create the embeddings for the three words:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22d30ec7",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "three_words_emb = co.embed(texts=list(three_words['text']),\n",
    "                           model='embed-english-v2.0').embeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f179777",
   "metadata": {
    "height": 63
   },
   "outputs": [],
   "source": [
    "word_1 = three_words_emb[0]\n",
    "word_2 = three_words_emb[1]\n",
    "word_3 = three_words_emb[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69da1290",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "word_1[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81adb0d9",
   "metadata": {},
   "source": [
    "## Sentence Embeddings"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "575da3a8",
   "metadata": {},
   "source": [
    "Consider a very small dataset of three sentences."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca6539bc",
   "metadata": {
    "height": 233
   },
   "outputs": [],
   "source": [
    "sentences = pd.DataFrame({'text':\n",
    "  [\n",
    "   'Where is the world cup?',\n",
    "   'The world cup is in Qatar',\n",
    "   'What color is the sky?',\n",
    "   'The sky is blue',\n",
    "   'Where does the bear live?',\n",
    "   'The bear lives in the the woods',\n",
    "   'What is an apple?',\n",
    "   'An apple is a fruit',\n",
    "  ]})\n",
    "\n",
    "sentences"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05733ed4",
   "metadata": {},
   "source": [
    "Let's create the embeddings for the three sentences:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef89a105",
   "metadata": {
    "height": 131
   },
   "outputs": [],
   "source": [
    "emb = co.embed(texts=list(sentences['text']),\n",
    "               model='embed-english-v2.0').embeddings\n",
    "\n",
    "# Explore the 10 first entries of the embeddings of the 3 sentences:\n",
    "for e in emb:\n",
    "    print(e[:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c33c078",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "len(emb[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2b96e44",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "#import umap\n",
    "#import altair as alt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eeb8c945",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "from utils import umap_plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de8a8509",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "chart = umap_plot(sentences, emb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93a581c4",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "chart.interactive()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cfb0192",
   "metadata": {},
   "source": [
    "## Articles Embeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfff2ced",
   "metadata": {
    "height": 63
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "wiki_articles = pd.read_pickle('wikipedia.pkl')\n",
    "wiki_articles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9bde94a",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from utils import umap_plot_big"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "874cf116",
   "metadata": {
    "height": 97
   },
   "outputs": [],
   "source": [
    "articles = wiki_articles[['title', 'text']]\n",
    "embeds = np.array([d for d in wiki_articles['emb']])\n",
    "\n",
    "chart = umap_plot_big(articles, embeds)\n",
    "chart.interactive()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "387c8901",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc3c9b2d",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8b338c7",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9e30d7b",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec708a6b",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
